package dawn.jxufe.community.controller;

import dawn.jxufe.community.entity.DiscussPost;
import dawn.jxufe.community.entity.User;
import dawn.jxufe.community.service.DiscussPostService;
import dawn.jxufe.community.service.LikeService;
import dawn.jxufe.community.service.UserService;
import dawn.jxufe.community.entity.Page;
import dawn.jxufe.community.util.CommunityConstant;
import dawn.jxufe.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dawn21
 * @version 1.0
 * @date 2022/7/6 16:14
 * @component
 */
@Controller
public class HomeController implements CommunityConstant {

    @Autowired
    private DiscussPostService discussPostService; //获取帖子。

    @Autowired
    private UserService userService; //通过获取帖子的用户id获取用户。

    @Autowired
    private LikeService likeService;

    @RequestMapping(path = "/", method = RequestMethod.GET)
    public String root() {
        return "forward:/index";
    }

    @RequestMapping(path = "/index", method = RequestMethod.GET)
    public String getIndexPage(Model model, Page page,
                               @RequestParam(name = "orderMode", defaultValue = "0") int orderMode) {
        /**
         * 方法调用前，springMVC会自动实例化Model和Page,并将Page注入到Model中，
         * 所以在thymeleaf模板中可以直接访问Page中的对象。
         */
        page.setRows(discussPostService.findDiscussPostRows(0));//设置总行数
        page.setPath("/index?orderMode=" + orderMode);

        //将查询的帖子结果赋值给list集合。
        List<DiscussPost> list = discussPostService.findDiscussPosts(0, page.getOffset(), page.getLimit(), orderMode);
        //创建一个可以含有用户名的map，将map放在list中。
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        //遍历list集合，得到用户id，来获取用户名。
        if(list != null){
            for(DiscussPost post : list){
                Map<String, Object> map = new HashMap<>();//键，值。放两对。
                map.put("post",post);//放入帖子。
                User user = userService.findUserById(post.getUserId());
                map.put("user",user);//放入用户。

                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                discussPosts.add(map);
            }
        }
        //将含有用户的list集合放入model。
        model.addAttribute("discussPosts",discussPosts);
        model.addAttribute("orderMode", orderMode);
        return "/index";
    }

    @RequestMapping(path = "/error", method = RequestMethod.GET)
    public String getErrorPage() {
        return "/error/500";
    }

    @RequestMapping(path = "/denied", method = RequestMethod.GET)
    public String getDeniedPage() {
        return "/error/404";
    }
}
